package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.GroupRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author wzh
 * @email 785579091@gmail.com
 * @date 2021-02-22 12:06:18
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelations(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrGroupService.getNoAttrRelations(params,attrGroupId);
        return R.ok().put("page",page);
    }


    @GetMapping("/{attrGroupId}/attr/relation")
    public R listAttrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrEntities = attrGroupService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrEntities);
    }

    @GetMapping("/{catelogId}/withattr")
    public R listWithAttr(@PathVariable("catelogId") Long catelogId){
        List<GroupRespVo> groupRespVoList = attrGroupService.getListWithAttr(catelogId);
        return R.ok().put("data",groupRespVoList);
    }


    @PostMapping("/attr/relation/delete")
    public R deleteRelations(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){

        relationService.deleteRelations(attrGroupRelationVos);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String,Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page",page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R relation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVoList){
        relationService.saveRelation(attrGroupRelationVoList);
        return R.ok();
    }

}
